package sismed.dados.hibernate.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import sismed.dao.IDAOExame;
import sismed.exception.DAOException;
import sismed.hibernate.util.HibernateUtil;
import sismed.negocio.modelo.Exame;

public class DAOHibernateExame implements IDAOExame {

	private Session session;

	public DAOHibernateExame() {
		super();
		this.session = HibernateUtil.getSession();
	}


	public void atualizarExame( Exame exame) throws DAOException{
		try{
			System.out.println("DOAHibernate::atualizarExame");
			this.session.beginTransaction();
			this.session.saveOrUpdate( exame );
			this.session.getTransaction().commit();
			this.session.clear();
		}
		catch (HibernateException e) {
			throw new DAOException( e );
		}
	}

	public void prescreverExame(Exame exame) throws DAOException {
		try {
			this.session.beginTransaction();
			this.session.save(exame);
			this.session.getTransaction().commit();
			this.session.clear();
		}
		catch (HibernateException e) {
			throw new DAOException( e );
		}

	}

	@SuppressWarnings("unchecked")
	public List<Exame> recuperarExamesPorFarmaceutico( int idfarmaceutico) throws DAOException {
		List<Exame> exames = null;
		try {
			exames = session.createQuery("FROM Exame WHERE farmaceutico_pessoa_id=" + idfarmaceutico + "").list();
			this.session.clear();
			
		}
		catch ( HibernateException e ) {
			throw new DAOException( e );
		}
		return exames;
	}

	@SuppressWarnings("unchecked")
	public List<Exame> recuperarExamesPorMedico(int idmedico) throws DAOException {
		List<Exame> exames = null;
		try {
			 exames = this.session.createQuery("FROM Exame WHERE medico_pessoa_id = " + idmedico + "").list();
			this.session.clear();
		}
		catch ( HibernateException e) {
			throw new DAOException(e);
		}
		return exames;
	}

	@SuppressWarnings("unchecked")
	public List<Exame> recuperarExamesPorPaciente(int idpaciente) throws DAOException{
		List<Exame> exames = null;
		try {
			exames = this.session.createQuery("FROM Exame WHERE paciente_pessoa_id = " + idpaciente+ "" ).list();
			this.session.clear();
		}
		catch ( HibernateException e ) {
			throw new DAOException( e );
		}
		return exames;
	}

	@SuppressWarnings("unchecked")
	public List<Exame> recuperarExamesPorTipo(String tipo) throws DAOException {
		List<Exame> exames = null;
		try {
			exames = this.session.createQuery("FROM Exame WHERE tipo = " + tipo + "").list();
			this.session.clear();
			
		}
		catch ( HibernateException e) {
			throw new DAOException( e );
		}
		return exames;
	}

	public Exame recuperarExame(String idexame) throws DAOException {
		Query query = null;
		try {
			System.out.println("RECUPERANDO EXAME :: " + idexame );
			query = session.createQuery("FROM Exame WHERE id='" + idexame + "'");
			this.session.clear();
		}
		catch ( HibernateException e ) {
			throw new DAOException( e );
		}
		return (Exame)query.uniqueResult();
	}

	public void atualizarExame(String idExame, Exame exame) throws DAOException{
	}
}

